#include "key.h"

uint8_t Key_read(void)
{
    if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) == 0)
        return  1;
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0)
        return  2;
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0)
        return  3;
    else if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0)
        return  4;
    else
        return 0;
}

uint8_t PWM_state;//0低电平，1：2KHz,20%
uint8_t key_now, key_up, key_down, key_old;
void Key_proc(void)
{
    key_now = Key_read();
    key_down = key_now & (key_now ^ key_old);
    key_old = key_now;

    if(key_down)
	{
		switch(key_down)
		{
			case 1:
                Show_mode ^= 1;
                break;
			case 2:
                if (Show_mode)
                {
                   charge_rate[0] += 0.5;
                   charge_rate[1] += 0.5;
                }
                    

                    break;
            case 3:
                if (Show_mode)
                {
                    charge_rate[0] -= 0.5;
                    charge_rate[1] -= 0.5;
                }
                    break;
            case 4:
                PWM_state ^= 1;
                if (PWM_state)
                {
                    Tim_set_freq(2000);
                    Tim_set_duty(20);
                }
                else
                {
                    Tim_set_freq(0);
                    Tim_set_duty(0);
                }

                    break;
        }
	}
}

